# -*- coding:utf-8
import datetime
#import logging
import re
import settings
import os

import pdb

from django.db import models
from django.contrib.auth.models import User
from util import get_unique_slug, nicely_remove_all_html
from django.utils.text import truncate_words
    
class BandMembership(models.Model):
    member = models.ForeignKey('Person')
    band = models.ForeignKey('Artist')
    start_year = models.CharField(max_length=30, blank=True, null=True)
    end_year = models.CharField(max_length=30, blank=True, null=True)
    instruments = models.CharField(max_length=500, blank=True)
    
    def __unicode__(self):
        return 'stint of %s in %s from %s to %s' % (self.member,
            self.band, self.start_year, self.end_year)
        
class Artist(models.Model):
    title = models.CharField(max_length=500)
    slug = models.SlugField()
    #short_text = models.TextField(blank=True)                     
    full_text = models.TextField(blank=True)
    picture = models.ImageField(upload_to='band-photos', blank=True)
    members = models.ManyToManyField('Person', blank=True, through=BandMembership)
    year_formed = models.CharField(max_length=4, blank=True)
    
    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = get_unique_slug(Artist, self.title)
        super(Artist, self).save(*args, **kwargs)
    
    def __unicode__(self):
        return self.title
        
    def get_absolute_url(self):
        return '/%s/' % self.slug

    class Meta:
        ordering = ['year_formed']
        verbose_name = 'Band'

class Album(models.Model):
    title = models.CharField(max_length=500)
    slug = models.SlugField()
    artist = models.ForeignKey(Artist)
    short_text = models.TextField(blank=True)
    full_text = models.TextField(blank=True)
    picture = models.ImageField(upload_to='album-covers', blank=True)
    year = models.CharField(max_length=4, blank=True)
    
    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = get_unique_slug(Album, self.title)
        super(Album, self).save(*args, **kwargs)
    
    def __unicode__(self):
        return self.title
        
    def get_absolute_url(self):
        return '/%s/%s/' % (self.artist.slug, self.slug)

    class Meta:
        ordering = ['year']

class Track(models.Model):
    mp3 = models.FileField(upload_to=settings.MP3_UPLOAD_PATH)
    title = models.CharField(max_length=500)
    slug = models.SlugField()
    text = models.TextField(blank=True)
    album = models.ForeignKey(Album)
    number = models.IntegerField(default=0, blank=True)
    duration = models.TimeField(null=True, blank=True)
    lyrics = models.TextField(blank=True)
    lyric_image = models.ImageField(upload_to=settings.IMAGE_UPLOAD_PATH, blank=True)
    hide_download_link = models.BooleanField(default=False)
    
    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = get_unique_slug(Track, self.title)
        super(Track, self).save(*args, **kwargs)
    
    def has_lyrics(self): 
        return self.lyrics or self.lyric_image
        
    def get_lyrics_html(self):
        retstr = ''
        if self.lyric_image:
            retstr += '<img src="/%s" alt="%s">' % (self.lyric_image, 
                'lyrics of %s' % (self.verbose_name()))
        if self.lyrics:
            retstr += self.lyrics
        return retstr
    
    def verbose_name(self):
        return '%s by %s' % (self.title, self.album.artist.title)
    
    def __unicode__(self):
        return "%s (%s, %s)" % (self.title, self.album, self.album.artist)
        
    def get_absolute_url(self):
        return '/%s/%s/%s/' % (self.album.artist.slug, self.album.slug, self.slug) 
    def get_lyrics_link(self):
        #return '%slyrics/' % self.get_absolute_url()
        return '/%s/lyrics/%s/' % (self.album.artist.slug, self.slug)
    
    class Meta:
        ordering = ['number']
    
    
class Person(models.Model):
    name = models.CharField(max_length=255)
    slug = models.SlugField()
    bio = models.TextField(blank=True)
    picture = models.ImageField(blank=True, upload_to='band-photos')
    
    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        super(Person, self).save(*args, **kwargs)
    
    def get_absolute_url(self):
        return '/biography/%s/' % self.slug

    def __unicode__(self):
        return self.name
        
    class Meta:
        verbose_name_plural='People'
    
class PostCategory(models.Model):
    name = models.CharField(max_length=100)
    
    def __unicode__(self):
        return self.name
    
class Post(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField()
    text = models.TextField()
    date = models.DateField(auto_now=True)
    picture = models.ImageField(blank=True, upload_to='post-pics')
    category = models.ForeignKey(PostCategory)
    user = models.ForeignKey(User, blank=True, editable=False)
    
    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        super(Post, self).save(*args, **kwargs)
    
    def get_short_text(self):
        text_only = nicely_remove_all_html(self.text)
        return truncate_words(text_only, 50)
    
    def __unicode__(self):
        return self.title
    
    def get_absolute_url(self):
        return '/posts/%s/%02d/%s/' % (
            self.date.year,
            self.date.month,
            self.slug)
        

    

    
    

    

